package com.admission.service;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.ProvinceFormDao;
import com.admission.model.ProvinceForm;
import com.admission.util.DeleteFile;

@Repository
@Transactional
public class ProvinceFormManager {
	private ProvinceFormDao provinceFormDao;
	private CollegeProvinceManager collegeProvinceManager;
	
	public void save(ProvinceForm entity,String sub){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		if (sub != null) {
			File file = new File(serverrealPath + File.separator
					+ entity.getUrl());
			DeleteFile.deleteByFile(file);// 删除源文件	
			entity.setUrl(sub);
		}
		provinceFormDao.save(entity);
	}
	
	/**
	 * 批量删除
	 * 逗号隔开id集合
	 * @param ids
	 */
	public void deleteByIds(String ids){
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		for(String id:ids.split(",")){
			ProvinceForm entity=this.getById(id.trim());
			File file = new File(serverrealPath + File.separator
						+ entity.getUrl());
			DeleteFile.deleteByFile(file);// 删除源文件
			collegeProvinceManager.deleteByIds(id.trim());
			provinceFormDao.delete(id.trim());
		}
	}
	/**
	 * 通过id查找位置对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public ProvinceForm getById(String id) {
		return provinceFormDao.findUniqueBy("id", id);
	}
	
	
	/**
	 * 通过filter分页查询
	 * 
	 * @param filters
	 * @return
	 */
	public Page<ProvinceForm> getPage(Page<ProvinceForm> page,List<PropertyFilter> filters) {
		return provinceFormDao.findPage(page,filters);
	}

	public ProvinceFormDao getProvinceFormDao() {
		return provinceFormDao;
	}
	@Autowired
	public void setProvinceFormDao(ProvinceFormDao provinceFormDao) {
		this.provinceFormDao = provinceFormDao;
	}

	public CollegeProvinceManager getCollegeProvinceManager() {
		return collegeProvinceManager;
	}
	@Autowired
	public void setCollegeProvinceManager(
			CollegeProvinceManager collegeProvinceManager) {
		this.collegeProvinceManager = collegeProvinceManager;
	}
}
